package com.android.qudian;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.android.qudian.cache.DiskLruCacheHelper;
import com.android.qudian.model.group.MainSearchSortData;
import com.android.qudian.model.group.MainSearchSortDataCallback;
import com.android.qudian.service.WebSocketService;
import com.android.qudian.service.WebSocketService1;
import com.android.qudian.service.WebSocketService2;
import com.android.qudian.service.WebSocketService3;
import com.android.qudian.utils.ConstantsUrl;
import com.android.qudian.utils.HttpUtils;
import com.android.qudian.utils.MyLog;
import com.android.qudian.utils.ObjectUtils;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;


public class WelcomeActivity extends AppCompatActivity {
    public static WelcomeActivity intance = null;
    private WelcomeActivity mcontext;
    private DiskLruCacheHelper helper;
    private  Intent serviceIntent,serviceIntent1,serviceIntent2,serviceIntent3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        intance = this;
        mcontext = WelcomeActivity.this;

//        ObjectUtils.StatusBarWhiteBg(mcontext, false);
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        GetSortData();
        initData();
    }

    private void initData() {
        serviceIntent = new Intent(WelcomeActivity.this, WebSocketService.class);
        serviceIntent1 = new Intent(WelcomeActivity.this, WebSocketService1.class);
        serviceIntent2 = new Intent(WelcomeActivity.this, WebSocketService2.class);
        serviceIntent3 = new Intent(WelcomeActivity.this, WebSocketService3.class);
        startService(serviceIntent);
        startService(serviceIntent1);
        startService(serviceIntent2);
        startService(serviceIntent3);
        Timer timer = new Timer();


        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                SharedPreferences myPref = getSharedPreferences(ConstantsUrl.SHAREDPREFERENCES_NAME, 0);
                boolean isGuide = myPref.getBoolean(ConstantsUrl.CACHE_HOME_GUIDE, false);
                if(isGuide){
                    startActivity(new Intent(mcontext, MainActivity.class));
                }else {
                    startActivity(new Intent(mcontext, GuideActivity.class));
                }
                finish();

            }
        }, 4000);
    }

    private void GetSortData() {
        boolean networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.SORTLISTDATA;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new MainSearchSortDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {

                }

                @Override
                public void onResponse(MainSearchSortData response, int id) {
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH, response.toString());
                    }
                }
            });
        } else {

        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
//        stopService(serviceIntent);
//        stopService(serviceIntent1);
//        stopService(serviceIntent2);
//        stopService(serviceIntent3);
    }
}
